package com.huhao.wshare.repository;

import com.huhao.wshare.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    boolean existsByEmail(String email);
    boolean existsByPhone(String phone);
    boolean existsByAccount(String account);

    Optional<User> findByEmail(String email);
    Optional<User> findByPhone(String phone);
    Optional<User> findByAccount(String account);

    // 用于登录时通过任一标识查找用户
    Optional<User> findByEmailOrPhoneOrAccount(String email, String phone, String account);

    /**
     * 新增方法：在应用启动时，重置所有用户的登录状态为 0 (未登录)
     * @Modifying 表示这是一个修改操作 (UPDATE, DELETE, INSERT)
     * @Transactional 保证操作的事务性
     */
    @Modifying
    @Transactional
    @Query("UPDATE User u SET u.isLogin = 0")
    void resetAllLoginStatuses();
}
